<?php
	namespace Qwadmin\Controller;
	use Think\Controller;
	class ItemnoController extends ComController{
		public $db;
		public $idb;
		public function __construct(){
			parent::__construct();
			$this->idb=M('Item_no');
			$this->db=M('Vendors');
		}
		public function index(){
			$info=$this->idb->select();
			$count=count($info);
			$Page= new \Think\Page($count,25);
        	if($_GET['p']<1){
            	$_GET['p']=1;
        	}else{
            	$_GET['p']=(int)$_GET['p'];//
        	}
        	$list=array_slice($info, 25*($_GET['p']-1),25);
        	$show=$Page->show();
        	$this->assign('page',$show);
			$this->assign("info",$list);
			$this->display();
		}
		public function add_item(){
			$post['item_no']=I('post.item_no');
			if($post['item_no']){
				if($this->ist($post['item_no'])){
					$re['status']=false;
					$re['con']="料号重复";
					$this->error("添加失败".$re['con']);
				}else{
					if($_FILES['image']['name']){
							$upload = new \Think\Upload();// 实例化上传类
		    				$upload->maxSize   =     3145728 ;// 设置附件上传大小
		    				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    				$upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
		    				$upload->savePath  =     ''; // 设置附件上传（子）目录
		    				$info   =   $upload->upload();
		    				if(!$info) {// 上传错误提示错误信息
		    					$this->error($upload->getError());
							}else{// 上传成功 获取上传文件信息
		    					foreach($info as $file){
		        					$post['pic_path']=$file['savepath'].$file['savename'];
		    					}
							}
						}
					$post['created_by']=$this->uid();
					$post['creation_date']=time();
					$post['item_desc']=I('post.item_desc');
					$post['item_category']=I('post.item_category');
					$post['disable_flag']=I('post.disable_flag');
					$post['units']=I('post.units');//单位 =table
					$post['unit_price']=I('post.unit_price');
					$post['safe_qty']=I('post.safe_qty');//安全库存
					$post['po_price']=I('post.po_price');//采购价格
					$post['supplier_name']=I('post.supplier_name');//供应商代码===table
					$post['min_order']=I('post.min_order');//
					$id=$this->idb->add($post);
					if($id){
						$this->success("添加成功!");
					}else{
						$this->error("添加失败".$re['con']);
					}
				}
			}else{
				$this->display();
			}
		}
		protected function ist($name){
			$where['item_no']=$name;
			$n=$this->idb->where($where)->select();
			return $n;
		}
		public function is_itemno(){
			$name=I('get.name');
			if($name){
				$where['item_no']=$name;
				$ba=$this->idb->where($where)->select();
				if($ba){
					$re['status']=true;
				}else{
					$re['status']=false;
				}
			}else{
					$re['status']=false;
			}
			$this->ajaxReturn($re);
		}
		public function change_item(){
			$id=(int)I('get.id');
			$info=$this->is_valid_item($id);
			if($info){
					$post['item_desc']=I('post.item_desc');
					$info=$info[0];
					if($post['item_desc']){
						if($_FILES['image']['name']){

							$upload = new \Think\Upload();// 实例化上传类
		    				$upload->maxSize   =     3145728 ;// 设置附件上传大小
		    				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    				$upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
		    				$upload->savePath  =     ''; // 设置附件上传（子）目录
		    				$info   =   $upload->upload();
		    				if(!$info) {// 上传错误提示错误信息
		    					$this->error($upload->getError());
							}else{// 上传成功 获取上传文件信息
		    					foreach($info as $file){
		        					$post['pic_path']=$file['savepath'].$file['savename'];
		    					}
							}

						}
						$where['item_id']=$id;

						$post['item_id']=$id;
						$post['item_desc']=I('post.item_desc');
						$post['item_category']=I('post.item_category');
						$post['disable_flag']=I('post.disable_flag');
						$post['units']=I('post.units');//单位 =table
						$post['unit_price']=I('post.unit_price');
						$post['safe_qty']=I('post.safe_qty');//安全库存
						$post['po_price']=I('post.po_price');//采购价格
						$post['min_order']=I('post.min_order');//
						$post['supplier_name']=I('post.supplier_name');//供应商代码===table  mini_order
						$id=$this->idb->where($where)->save($post);
						if($id){
							$this->success("修改成功!");
						}else{
							$this->error("修改失败");
						}
					}else{
						$this->assign("info",$info);
						$this->display();
					}
			}
		}
		public function search(){
			$desc=I('get.desc');
			$no=I('get.no');
			$supplier_name=I('get.supplier_name');
			$where['item_no']=array('LIKE',"%".$no."%");
			$where['item_desc']=array('LIKE',"%".$desc."%");
			$where['supplier_name']=array('LIKE',"%".$supplier_name."%");

			$info=$this->idb->where($where)->select();
			$count=count($info);
			$Page= new \Think\Page($count,25);
        	if($_GET['p']<1){
            	$_GET['p']=1;
        	}else{
            	$_GET['p']=(int)$_GET['p'];//
        	}
        	$list=array_slice($info, 25*($_GET['p']-1),25);
        	$show=$Page->show();
        	$this->assign('page',$show);
			$this->assign("info",$list);
			$this->display();
		}
		function achieve(){
			$desc=I('get.de');
			$no=I('get.no');
			$supplier_name=I('get.vo');
			$where['item_no']=array('LIKE',"%".$no."%");
			$where['item_desc']=array('LIKE',"%".$desc."%");
			$where['supplier_name']=array('LIKE',"%".$supplier_name."%");
			$info=$this->idb->where($where)->select();
			$this->ajaxReturn($info);
		}
		function page(){
			$page=I('get.page');
			$info=$this->idb->select();
        	if($page<1){
            	$page=1;
        	}else{
            	$page=(int)$page;//
        	}
        	$list=array_slice($info, 10*($page-1),10);
        	$this->ajaxReturn($list);

		}
		public function delete_item(){
			$id=(int)I('get.id');
			if($this->is_valid_item($id)){
				$where['item_id']=$id;
				$re['status']=$this->idb->where($where)->delete();
			}else{
				$re['status']=false;
				$re['con']="没有找到该信息！";
			}
			$this->ajaxReturn($re);
		}
		public function id_item(){
			$id=(int)I('get.id');
			$info=$this->is_valid_item($id);
			if($info){
				$this->ajaxReturn($info[0]);
			}else{
				$this->ajaxReturn($info);
			}
		}
		public function id(){
			$id=(int)I('get.id');
			$info=$this->is_valid_item($id);
			if($info){
				$this->assign("info",$info[0]);
				$this->display();
			}else{
				$this->error('没有该信息');
			}
		}
		public function is_valid_item($id){//获取id对应的内容
			$id=(int)$id;
			$where['item_id']=$id;
			return $this->idb->where($where)->select();
		}
		public function uid(){
			$uid=$_SESSION['uid'];
			$uid=(int)$uid;
			$info=M('Member')->find($uid);
			return $info['user'];
		}


	}